<?php
/*
 * Created on 25.01.2006
 * 
 */

m2import('phpinchains/Filter.php');
m2import('phpinchains/psp/OutputWriter.php');
m2import('phpinchains/tagsupport/URL.php');

/**
 * ViewDispatcher class. can invoke a psp
 */
class ViewDispatcher {

    protected $config;
    protected $initbasedir;
    protected $relativeFilePath;

    /**
     * Constructor
     *
     * @param string                 $psprootdir Root dir of the psp pages
     * @param array                  $namespaces Namespaces to limit the parser to. Tags of other namespaces are ignored.
     * @param phpinchains_ActionForm $actionform Action form instance to use, if given.
     */
    function __construct($psprootdir, $namespaces) {
        $this->config['psp-root-dir'] = $psprootdir;
        $this->config['namespaces']   = $namespaces;
        $this->initbasedir            = $this->config['psp-root-dir'];
    }

    /**
     * Dispatch the given psp page
     *
     * @param string $psppage Psp page to process
     */
    function dispatch($psppage, & $actionform = null) {
        $this->config['psp-page']     = $psppage;

        $ctx = phpinchains_ApplicationContext::getInstance();
        if ($actionform == null)
            $actionform = new phpinchains_ActionForm();
        $out = new phpinchains_psp_OutputWriter();
        $env = $ctx->getEnv();
        $dispatcher = $this;
        if ($this->config['psp-root-dir']) {
            $pspfile = "";
            if ($this->config['psp-page']) {
                if (substr($this->config['psp-page'], -1, 1) == '/') {
                    $this->relativeFilePath = '/'.$this->config['psp-page'].'index.psp';
                    $pspfile = $this->config['psp-root-dir'].'/'.$this->config['psp-page'].'index.psp';
                } elseif (substr($this->config['psp-page'], -4, 4) == '.psp') {
                    $this->relativeFilePath = '/'.$this->config['psp-page'];
                    $pspfile = $this->config['psp-root-dir'].'/'.$this->config['psp-page'];
                }
            }
            else {
                $this->relativeFilePath = '/index.psp';
                $pspfile = $this->config['psp-root-dir'].'/index.psp';
            }
            if (file_exists($pspfile)) {
                PHPinChains::compileFile($pspfile, $this->config['namespaces'], 'psp');
                include($pspfile.'.php');
            } else {
                return "ViewDispatcher: Template not found: ".$pspfile;
            }
        }
        else {
            return "ViewDispatcher configuration error: No psp-root-dir specified!";
        }
        return $out->getBuffer();
    }

    /**
     * Compiles the PSP and saves the result for further execution.
     * @param string sfile Source file name
     * @param string cfile Compiled destination file name
     */
    function compilePSP($sfile, $cfile) {
        PHPinChains::compileFile($sfile, $this->config['namespaces'], 'psp');
    }

    function getTagInstance($namespace, $name) {
        $classname = $name.'Handler';
        if (!class_exists($namespace."_".$name)) {
            m2import('phpinchains/tags/'.$namespace."/".$classname.'.php');
        }
        $classname = $namespace."_".$classname;
        return new $classname;
    }

    /**
     * Getter for initial basedir
     *
     * @return string initial basedir
     */
    function getInitialBaseDir() {
        return $this->initbasedir;
    }

    /**
     * Compile dir set? False, because ViewDispatcherFilter doesn't support this feature at the moment.
     *
     * @return boolean If compile dir was set
     */
    function hasCompileDir() {
        return false;
    }

    /**
     * Get class name for psp class derived from absolute filename
     *
     * @param string $filename Absolute file name of psp file
     */
    function getPSPClassName($filename) {
        return str_replace('/', '__', $filename);
    }

    function isPSPClassCompiler () {
        return false;
    }

}
?>